import { Inject, Query } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { ArticleArticleEntity } from '../../entity/article';
import { ArticleArticleService } from '../../service/article';
import { ArticleCategoryEntity } from '../../entity/category';

/**
 * 文章管理
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'page'],
  entity: ArticleArticleEntity,
  service: ArticleArticleService,
  pageQueryOp: {
    keyWordLikeFields: ['a.title'],
    fieldEq: ['a.categoryId', 'a.status'],
    select: ['a.*', 'b.name as categoryName'],
    join: [
      {
        entity: ArticleCategoryEntity,
        alias: 'b',
        condition: 'a.categoryId = b.id',
        type: 'leftJoin',
      },
    ],
    where: async ctx => {
      const { startTime, endTime } = ctx.request.body;
      const where = [];
      if (startTime && endTime) {
        where.push([
          'a.createTime BETWEEN :startTime AND :endTime',
          { startTime, endTime },
        ]);
      }
      return where;
    },
  },
})
export class AdminArticleArticleController extends BaseController {
  @Inject()
  articleArticleService: ArticleArticleService;
}
